package org.angelica.modules.common;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.angelica.core.common.AccessToken;
import org.angelica.core.response.ResponseResult;
import org.angelica.modules.account.service.AccUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author aizhimin
 */
@Api(tags = {"登录注册"})
@RestController
@RequestMapping(value="/api")
public class LoginController {

    @Autowired
    private AccUserService accUserService;

    @ApiOperation(value = "登录",notes = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = false, dataType = "string", paramType="query"),
            @ApiImplicitParam(name = "password", value = "密码", required = false, dataType = "string", paramType="query"),
    })
    @PostMapping(value="/login")
    public ResponseResult login(@RequestParam String username,
                                @RequestParam String password) {
        AccessToken accessToken = accUserService.login(username,password);
        return ResponseResult.success("accessToken",accessToken);
    }

    @ApiOperation(value = "注册",notes = "注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = false, dataType = "string", paramType="query"),
            @ApiImplicitParam(name = "password", value = "密码", required = false, dataType = "string", paramType="query"),
            @ApiImplicitParam(name = "nickname", value = "昵称", required = false, dataType = "string", paramType="query"),
    })
    @PostMapping(value="/regist")
    public ResponseResult regist(@RequestParam String username,
                                 @RequestParam String password,
                                 @RequestParam String nickname) {
        AccessToken accessToken = accUserService.regist(username,password,nickname);
        return ResponseResult.success("accessToken",accessToken);
    }

}
